package com.bailiban.aigou.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bailiban.aigou.common.BaseAction;
import com.bailiban.aigou.entity.User;
import com.bailiban.aigou.service.IUserService;
import com.bailiban.aigou.utils.PageModel;

@Controller
@Scope("prototype")
@Namespace("/admin/user")
public class UserAction extends BaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IUserService userService;

	/**
	 * 用户分页数据
	 * @return
	 */
	@Action(value = "list", results = {
		@Result(name = SUCCESS, location = SYS_JSP_BASEURL + "admin/user/list.jsp")	
	})
	public String list(){
		HttpServletRequest request = ServletActionContext.getRequest();
		super.setPageNo(request.getParameter("pageNo"));
		super.setPageSize(request.getParameter("pageSize"));
		
		PageModel<User> pageModel = userService.getAdminUser(super.getPageNo(), super.getPageSize());
		request.setAttribute("userPages", pageModel);
		return SUCCESS;
	}
	
	/**
	 * 用户添加界面
	 * @return
	 */
	@Action(value = "addPage", results = {
			@Result(name = SUCCESS, location = SYS_JSP_BASEURL + "admin/user/add.jsp")	
	})
	public String addPage(){
		return SUCCESS;
	}
	
	@Action(value = "add")
	public String add(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		
		User user = new User();
		user.setUsername(userName);
		user.setPassword(password);
		userService.save(user);
		
		request.setAttribute("url", "admin/user/list");
		return SYS_ACTION_CLOSE;
	}
}
